﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BrickAndBrick
{

    class BBController
    {
        /// <summary>
        /// 找出当前Map中所有可消除的砖块
        /// </summary>
        /// <param name="map"></param>
        /// <returns></returns>
        public static List<BBStep> ScanAllAvailable(BBMap map)
        {
            List<BBStep> stepList = new List<BBStep>();
            for (int i = 0; i < (BBMap.COL * BBMap.ROW); i++)
            {
                BBStep step = SingleScan(map, i / BBMap.COL, i % BBMap.COL);
                if (step != null)
                {
                    stepList.Add(step);
                }
            }
            return stepList;
        }

        public static List<BBStep> BeginScan(BBMap map)
        {
            List<BBStep> stepList = new List<BBStep>();
            BBStep tempStep = new BBStep(map);
            //tempStep.scanPos = new Random().Next(-1, 138);
            stepList.Add(tempStep);
            Console.WriteLine(stepList[0].map.ToString());
            while (true)
            {
                WriteStepList(stepList);
                BBStep lastStep = stepList[stepList.Count - 1];
                BBStep nextStep;
                //Console.WriteLine("List.Count:" + stepList.Count +" lastPos:" + lastStep.scanPos);
                nextStep = ScanMap(lastStep.scanPos + 1, lastStep.map);
                if (nextStep != null)
                {
                    //Console.WriteLine(nextStep.map.ToString());
                    stepList[stepList.Count - 1].scanPos = nextStep.movePos;
                    stepList.Add(nextStep);
                    if (nextStep.map.IsEmpty())
                    {
                        stepList.RemoveAt(0);
                        Console.WriteLine("通关成功");
                        foreach (var item in stepList)
                        {
                            Console.WriteLine(item.ToString());
                        }
                        return stepList;
                    }
                }
                else
                {
                    stepList.RemoveAt(stepList.Count - 1);
                }
                if (stepList.Count == 1)
                {
                    Console.WriteLine("游戏失败");
                    break;
                }
            }
            return null;
        }

        static void WriteStepList(List<BBStep> stepList)
        {
            for (int i = 0; i < stepList.Count; i++)
            {
                Console.Write(stepList[i].scanPos + " ");
            }
            Console.WriteLine();
        }

        static BBStep ScanMap(int startPos, BBMap map)
        {
            for (int i = startPos; i < (BBMap.COL * BBMap.ROW); i++)
            {
                BBStep step = SingleScan(map, i / BBMap.COL, i % BBMap.COL);
                if (step != null)
                {
                    return step;
                }
            }
            return null;
        }


        public static BBStep SingleScan(BBMap bMap, int x, int y)
        {

            BBStep step = bMap.TryLeft(x, y);
            if (step != null)
            {
                return step;
            }
            step = bMap.TryUp(x, y);
            if (step != null)
            {
                return step;
            }
            step = bMap.TryRight(x, y);
            if (step != null)
            {
                return step;
            }
            step = bMap.TryDown(x, y);
            if (step != null)
            {
                return step;
            }
            return null;
        }
    }
}
